package com.yoomiito.app.ui.my.remain

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.gyf.barlibrary.ImmersionBar
import com.jakewharton.rxbinding3.view.clicks
import com.yoomiito.app.R
import com.yoomiito.app.adapter.my.RemainAdapter
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.my.ApplyInfo
import com.yoomiito.app.model.my.MyRemainMoneyInfo
import com.yoomiito.app.ui.my.psw.SetPayPswActivity
import com.yoomiito.app.ui.my.remain.detail.MyMoneyDetailActivity
import com.yoomiito.app.ui.my.remain.transfer.TransferActivity
import com.yoomiito.app.ui.my.remain.withdraw.WithdrawListActivity
import com.yoomiito.app.utils.AllUtils
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.EventMessage
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.utils.UserInfoConfig
import com.yoomiito.app.widget.SpacingDecoration
import com.yoomiito.app.widget.TipDialog
import kotlinx.android.synthetic.main.act_deduct_money.smartRefreshLayout
import kotlinx.android.synthetic.main.activity_my_remain.*
import kotlinx.android.synthetic.main.fm_rcy.fm_refreshLayout
import kotlinx.android.synthetic.main.view_my_order_top_remain.*
import kotlinx.android.synthetic.main.view_star_coin.*
import java.util.concurrent.TimeUnit

class MyRemainActivity : BaseActivity<MyRemainPresenter>() {
    private var mRemainMoney: String? = null
    private var type = ACTIVITY_TYPE_REMAIN
    private var mStarCoin: View? = null
    private var mAdapter: RemainAdapter? = null
    var empty: View? = null
    var page : Int = 1
    override fun getLayoutId(): Int {
        return R.layout.activity_my_remain
    }

    override fun newP(): MyRemainPresenter {
        return MyRemainPresenter(App.mAppConfig)
    }


    public override fun initImmersionBar() {
        ImmersionBar.with(this).keyboardEnable(false).init()
    }

    override fun onStart() {
        super.onStart()
//        showLoading()
        p.getIsAgent()
    }

    override fun initData(savedInstanceState: Bundle?) {
        type = intent.getIntExtra(KEY_ACTIVITY_TYPE, ACTIVITY_TYPE_REMAIN)
        mRemainMoney = intent.getStringExtra(REMAIN_MONEY_KEY)
        empty = LayoutInflater.from(context).inflate(R.layout.item_live_empty, null, false)
        val text = empty?.findViewById<TextView>(R.id.item_empty_tv)
        text?.text = "暂无提现信息"
        initTop()
        mAdapter = RemainAdapter()
        rcy.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        rcy.addItemDecoration(SpacingDecoration(0, DensityUtil.dp2px(1f), true))
        rcy.adapter = mAdapter
        mAdapter?.setOnItemClickListener { adapter, view, position ->

        }
        smartRefreshLayout.setOnRefreshListener {
            page = 1
            p.applyList(page,type)
        }
        smartRefreshLayout.setOnLoadMoreListener {
            page++
            p.applyList(page,type)
        }
        p.applyList(page,type);


    }

    private fun initStarCoin(data: MyRemainMoneyInfo) {
        if (!data.currency.isNullOrEmpty()) {
            if (mStarCoin == null) {
                mStarCoin = star_coin_vs.inflate()
            }

            starCoinTv.typeface = AllUtils.getTypeface()
            starCoinTv.text = data.currency
            starCoinDetailTv.clicks()
                    .throttleFirst(1, TimeUnit.SECONDS)
                    .subscribe {
                        MyMoneyDetailActivity.start(this, StarCoin,type)
                    }
        }
    }

    private fun initTop() {
        back.setOnClickListener { finish() }
        transfer.setOnClickListener {
            if (UserInfoConfig.havePayPsw()) {
                TransferActivity.start(this)
            } else {
                showSetPswDialog()
            }
        }
        btn_submit.setOnClickListener {
//            when{
//                UserInfoConfig.bindWX()->p!!.WXAuthorization()
//                !UserInfoConfig.havePayPsw()->showSetPswDialog()
//                else -> RemainActivity.start(this)
//            }
            WithdrawListActivity.start(this,type)
        }
        tv_money.typeface = AllUtils.getTypeface()
        tv_money.text = mRemainMoney
        tv_des.text = "可提现余额(元)"
        account_detail.clicks()
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe {
                    MyMoneyDetailActivity.start(this, Remain,type)
                }
    }

    fun bindWX() {
//        if (UserInfoConfig.havePayPsw()) {
//            RemainActivity.start(this)
//        } else {
//            showSetPswDialog()
//        }
    }

    private fun showSetPswDialog() {
        if (!UserInfoConfig.havePayPsw()) {
            showSelectDialog(ResUtil.getString(R.string.my_tip),
                    "为确保余额资金安全，请先设置千年艾支付密码", "马上设置",
                    "取消",
                    object : TipDialog.Dialogcallback {
                        override fun btnConfirm() {
                            SetPayPswActivity.start(this@MyRemainActivity)
                        }

                        override fun cancel(): Boolean {
                            return false
                        }
                    })
        }
    }

    fun setIsAngent(data: MyRemainMoneyInfo) {
        dismissLoading()
        setRemainMoney(data.money)
        initStarCoin(data)
    }

    //设置金额
    public fun setRemainMoney(money: String) {
        dismissLoading()
        mRemainMoney = money
        tv_money.text = money
    }

    override fun eventMessage(eventMessage: EventMessage?) {
        super.eventMessage(eventMessage)
        if (eventMessage?.eventType.equals("withdraw")){
            page = 1
            p.applyList(page,type)
            p.getUserCenterData(type)
        }
    }


    fun setData(t: List<ApplyInfo>?) {
        dismissLoading()
        if (page == 1) {
            if (t.isNullOrEmpty()) {
                mAdapter?.emptyView = empty
            } else {
                mAdapter?.setNewData(t)
            }
            smartRefreshLayout.setNoMoreData(false)
        } else {
            smartRefreshLayout.finishLoadMore(true)
            if (t.isNullOrEmpty()) {
                Toast.makeText(context, "没有更多了", Toast.LENGTH_SHORT).show()
                smartRefreshLayout.finishLoadMoreWithNoMoreData()
            } else {
                mAdapter?.addData(t)
            }
        }
        smartRefreshLayout.finishRefresh()
        smartRefreshLayout.finishLoadMore()
    }

    companion object {
        const val REMAIN_MONEY_KEY = "remain_money_key"
        const val ACTIVITY_TYPE_REMAIN = 1
        const val KEY_ACTIVITY_TYPE = "key_activity_type"
        const val Remain = "remain"
        const val StarCoin = "star_coin"
    }

}
